
******************
** Diesel Data **
******************

** take data of stations
clear
use "$P_Data/Price/Daily_AvgP_WA_7to21_diesel.dta", clear 


capture drop index

tostring YMD, replace force
gener YMD2 = YMD
destring YMD2, replace force
gen date=date(YMD, "YMD")
drop YMD
rename YMD2 YMD
format date %td
gener year = year(date)
gener month = month(date)
gener year_month =ym(year,month)

drop if year<2016
drop if year>2018


xtset StID date

replace AvgP = AvgP/1000


** merge in adopters & adoption dates
merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_diesel.dta"


gener break_date = avgdate if _merge==3
gener treat_group = 1 if _merge==3
replace break_date = 10^5 if _merge==1
replace treat_group = 0 if missing(treat_group)
drop _merge

capture drop Brand
capture drop Post


****

capture drop week
gener week = week(date)

gener weekly_date = yw(year,week)

gener treat_period = 1 if weekly_date>=break_date
replace treat_period = 0 if weekly_date<break_date


**** 

*** merge in regional wholesale prices

merge m:1 StID using "$P_Data_Processed/wholesale_price_concordance.dta" 
drop if _merge==2
drop _merge

merge m:1 date region using  "$P_Data_Processed/wholesale_price_diesel.dta"
drop _merge

**** compute margins
**  taxes
* https://www.eea.europa.eu/data-and-maps/indicators/fuel-prices-and-taxes/assessment-5
gener tax = 0.4704
* add VAT 
** VAT needs to be 19% in Germany
* 

gener margin_wholesale = AvgP/1.19 - mean_w_price 


* drop if avg price is too low... 
keep if AvgP>0


capture drop Name St StNum City Lat Lng nOccur
capture drop ID
capture drop Sig
capture drop Post
capture drop Brand
		
* merging in gas station info *
merge m:1 StID using "$P_Data/GS/gas_station.dta"
keep if _merge==3
drop _merge

drop Name St StNum City Lat Lng nOccur
drop ID


* merging in demographics
* first merge in concordance table
merge m:1 Post using "$P_Data_Processed/NUTS3_Post_concordance_table.dta"
drop if _merge==2
drop _merge
merge m:1 nuts3 year using "$P_Data_Processed/demog.dta"
drop if _merge==2
drop _merge
gener l_gdp = ln(GDP_current)
gener employed_share = employed_pop/tot_pop
gener l_pop = ln(tot_pop)


** merging in weather data **
merge m:1 StID using "$P_Data_Processed/weather_concordance.dta"
drop if _merge==2
drop _merge

merge m:1 sdo_id date using "$P_Data_Processed/weather_data.dta"
drop if _merge==2
drop _merge

* generating monthly avg weather vars 
gegen mean_temp = mean(temperature), by(StID year_month)
gegen sd_temp = sd(temperature), by(StID year_month)
gegen mean_precip = mean(precipitation), by(StID year_month)
gegen sd_precip = sd(precipitation), by(StID year_month)
gegen mean_snow = mean(snow), by(StID year_month)
gegen avg_wind_speed = mean(mean_wind_speed), by(StID year_month)

* generating other monthly variables 
fsort StID year_month date
by StID year_month: gener n_days_active_month = _N


gegen month_treat_period = max(treat_period), by(StID year_month)
gegen mean_price = mean(AvgP), by(StID year_month)
 
gegen mean_wh_margin = mean(margin_wholesale), by(StID year_month)


** labelling variables **

label variable mean_wh_margin "Mean Margin"

label variable mean_price "Mean Price"
** controls **
label variable l_gdp "ln(Regional GDP)"
label variable l_pop "ln(Total Regional Population)"
label variable employed_share "Regional Employment Share (employed/pop)"
label variable pop_density "Regional Population Density (pop/$km^2$)"
label variable med_age "Regional Median Population Age"

label variable mean_temp "Mean Temperature (degrees Celsius)"
label variable sd_temp "Std. Dev. Temperature (degrees Celsius)"
label variable mean_precip "Mean Precipitation (mm)"
label variable sd_precip "Std. Dev. Precipitation (mm)"

** collapsing to market data 

gegen min_date_StID = min(date), by(StID year_month)
keep if date==min_date_StID
drop min_date_StID

by year_month Post, sort: gener postal_n_stations = _N
gener postal_n_others = postal_n_stations-1
drop if Post==0

** defining treatment 

capture drop treat
gener treat = month_treat_period*treat_group
label variable treat "Adopter" 

**
** drop outlier observations (top and bottom 1%)
drop if mean_price>1.5
drop if mean_price<0.95
drop if mean_wh_margin<0
drop if mean_wh_margin>0.22

	

sort StID year_month
by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0
sort StID first_treat_month
by StID: replace first_treat_month = first_treat_month[1]
replace first_treat_month = 1000 if missing(first_treat_month)
	
* number of other adopters in your Postal code
by Post year_month, sort: egen n_compet_adopt = total(treat)
replace n_compet_adopt = n_compet_adopt - 1 if treat==1	
	
** generating simple IV - share of adopters in your brand
by Brand year_month, sort: gener n_brand = _N
by Brand year_month: egen n_treat = total(treat)

gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1

label variable n_brand "N Brand Stations"
label variable share_others_treated "Share Brand Adopters"
label variable postal_n_stations "N Competitors in ZIP"
label variable n_compet_adopt "N Competitors Adopting"

		
merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener station = 1 
gegen n_act_stations = total(station), by(mktid year_month)

gegen n_mkt_compet_adopt = total(treat), by(year_month mktid)
replace n_mkt_compet_adopt = n_mkt_compet_adopt - 1 if treat==1

capture drop n_months_since_treat 
sort StID year_month
capture drop age 
by StID: gener age = _n
gener n_months_since_treat = year_month - first_treat_month
replace n_months_since_treat=. if n_months_since_treat<-31




ivreghdfe mean_wh_margin postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = share_others_treated ) if year<2019, absorb(year_month StID) cluster(mktid)
sum mean_wh_margin if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G9",  tex(frag) dec(3) label nor2 replace keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

ivreghdfe mean_price postal_n_stations  l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip ( treat = share_others_treated ) if year<2019, absorb(year_month StID) cluster(mktid)
sum mean_price if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G9",  tex(frag) dec(3) label nor2 append keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)


  ivreghdfe mean_wh_margin mkt_n_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
 sum mean_wh_margin if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
outreg2 using "$P_Tables/Table_G9",  tex(frag) dec(3) label nor2 append keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)


 
 ivreghdfe mean_price  mkt_n_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations==1, absorb(year_month StID) cluster(mktid) 
sum mean_price if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
  outreg2 using "$P_Tables/Table_G9",  tex(frag) dec(3) label nor2 append keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)


 
  ivreghdfe mean_wh_margin mkt_n_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
  sum mean_wh_margin if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G9",  tex(frag) dec(3) label nor2 append keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)


 ivreghdfe mean_price  n_act_stations n_mkt_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & n_act_stations>1, absorb(year_month StID) cluster(mktid) 
sum mean_price if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
  outreg2 using "$P_Tables/Table_G9",  tex(frag) dec(3) label nor2 append keep(treat)  addstat(Non-Adopter Mean Outcome,outcome_var_mean)

  